package acme.db;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import acme.entity.Product;

import com.amazonaws.services.simpledb.model.Attribute;
import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
import com.amazonaws.services.simpledb.model.Item;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;
import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.SelectResult;

public class ProductDAOImpl extends SimpleDB implements CrudDao<Product>{
	private static final String DOMAIN = "Product";
	
	private static final String PRODUCT_ID = "ProductId";
	private static final String BRAND_ID = "BrandId";
	private static final String DEPT_ID = "DepartmentId";
	private static final String NAME = "Name";
	private static final String DESCRIPTION = "Description";
	
	
	public ProductDAOImpl() {
		super();
		initDomain(DOMAIN);
	}
	
	@Override
	public Class<Product> getClazz() {
		return Product.class;
	}

	@Override
	public Product createResource(Product prod) {
		PutAttributesRequest putAttributesRequest = new PutAttributesRequest();
		putAttributesRequest.setDomainName(DOMAIN);
		putAttributesRequest.setItemName(prod.getId());
		putAttributesRequest.setAttributes(this.store(prod));
		sdb.putAttributes(putAttributesRequest);
		return prod;
	}

	@Override
	public Collection<Product> getAllResources() {
		Collection<Product> collection = new ArrayList<Product>();
		SelectRequest selectRequest = new SelectRequest();
		String selectExpression = "select * from " + DOMAIN;
		selectRequest.setSelectExpression(selectExpression);
		SelectResult result = sdb.select(selectRequest);
		List<Item> list = result.getItems();
		for(Item item : list){
			Product prod = this.load(item.getAttributes());
			collection.add(prod);
		}
		return collection;
	}

	@Override
	public Product readResource(String productId) {
		Product prod = null;
		SelectRequest selectRequest = new SelectRequest();
		String selectExpression = "select * from " + DOMAIN + " where " + PRODUCT_ID + " = '" + productId + "'";
		selectRequest.setSelectExpression(selectExpression);
		SelectResult result = sdb.select(selectRequest);
		List<Item> list = result.getItems();
		if(list.size() > 0){
			Item item = list.get(0);
			prod = this.load(item.getAttributes());
		}
		return prod;
	}

	@Override
	public void updateResource(Product prod) {
		this.createResource(prod);
	}

	@Override
	public void deleteResource(String ticker) {
		DeleteAttributesRequest deleteAttributesRequest = new DeleteAttributesRequest(DOMAIN, ticker);
		sdb.deleteAttributes(deleteAttributesRequest);
	}

	private Collection<ReplaceableAttribute> store(Product prod){
		List<ReplaceableAttribute> list = new ArrayList<ReplaceableAttribute>();
		ReplaceableAttribute ra = new ReplaceableAttribute(PRODUCT_ID, prod.getId(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(NAME, prod.getName(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(BRAND_ID, prod.getBrandId(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(DEPT_ID, prod.getDepartmentId(), true);
		list.add(ra);
		ra = new ReplaceableAttribute(DESCRIPTION, prod.getDescription(), true);
		list.add(ra);
		return list;
	}
	
	private Product load(List<Attribute> list){
		Product product = new Product();
		for(Attribute ra : list){
			if(ra.getName().equals(PRODUCT_ID)){
				product.setProductId(ra.getValue());
				continue;
			}
			if(ra.getName().equals(BRAND_ID)){
				product.setBrandId(ra.getValue());
				continue;
			}
			if(ra.getName().equals(DEPT_ID)){
				product.setDepartmentId(ra.getValue());
				continue;
			}
			if(ra.getName().equals(NAME)){
				product.setName(ra.getValue());
				continue;
			}
			if(ra.getName().equals(DESCRIPTION)){
				product.setDescription(ra.getValue());
				continue;
			}
		}
		return product;
	}
	
}
